package com.yyr.callmessage.user.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yyr.callmessage.core.ro.BaseRo;
import com.yyr.callmessage.core.util.SMSUtil;
import com.yyr.callmessage.img.domain.service.VerificationImgService;
import com.yyr.callmessage.user.common.UserError;
import com.yyr.callmessage.user.domain.entity.User;
import com.yyr.callmessage.user.domain.service.UserService;
import com.yyr.callmessage.user.object.qo.GetSMSUserQo;
import com.yyr.callmessage.user.object.qo.RegisterAndLoginUserQo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Description:  user_login 用户（用户登录模块）
 * @Author：
 * @Date：2021/6/29
 */
public class LoginUserController {

    @Resource
    private UserService userService;

    @Resource
    private VerificationImgService verificationImgService;

    /**
     * @api {get} /1.0/user/login/sms 获取登录短信验证码
     * @apiName user_login_sms_get
     * @apiGroup user_login
     *
     * @apiParam {String} phone
     * @apiParam {String} verificationImg
     *
     * @apiUse headerAndAuthorization
     * @apiUse successResponse
     */
    @GetMapping(value = "/login/sms")
    public BaseRo getSMSVerification(@RequestBody @Valid GetSMSUserQo qo , HttpServletRequest request) throws Exception {
        //判断图片验证码是否与缓存的一致
        String verificationImg = verificationImgService.getVerificationImg(qo.getPhone());
        if (verificationImg == null || !qo.equals(verificationImg)){
            return new BaseRo(UserError.VerificationImgErrorCode , UserError.VerificationImgErrorMsg);
        }

        //判断该手机号是否注册
        User user = userService.getByPhone(qo.getPhone());
        if (null == user){
            return new BaseRo(UserError.UserPhoneHasNotExistCode , UserError.UserPhoneHasNotExistMsg);
        }
        //随机生成四位数并调用第三方接口
        String randomParam = userService.getRandomNumber();
        String resultJson = SMSUtil.sendVerification(qo.getPhone() , randomParam);
        Gson gson = new GsonBuilder().create();
        BaseRo result = gson.fromJson(resultJson , BaseRo.class);
        //短信返回码为0，则发送成功
        if (!"0".equals(result.getCode())){
            return new BaseRo(UserError.UserThirdSMSErrorCode , UserError.UserThirdSMSErrorMsg);
        }

        //添加短信验证码缓存
        userService.addVerificationSMS(qo.getPhone() , randomParam);
        return new BaseRo();
    }

    /**
     * @api {post} /1.0/user/login 手机号登陆
     * @apiName user_login_post
     * @apiGroup user_login
     *
     * @apiParam {String} phone
     * @apiParam {String} verificationSMS
     *
     * @apiUse headerAndAuthorization
     * @apiUse successResponse
     */
    @PostMapping(value = "/login")
    public BaseRo login(@RequestBody @Valid RegisterAndLoginUserQo qo){
        //判断短信验证码是否与缓存的一致
        String verificationSMS = userService.getVerificationSMS(qo.getPhone());
        if (verificationSMS == null || !qo.equals(verificationSMS)){
            return new BaseRo(UserError.VerificationSMSErrorCode , UserError.VerificationSMSErrorMsg);
        }

        //判断该手机号是否注册
        User user = userService.getByPhone(qo.getPhone());
        if (null == user){
            return new BaseRo(UserError.UserPhoneHasNotExistCode , UserError.UserPhoneHasNotExistMsg);
        }
        return new BaseRo();
    }

}
